package zhr.chapter9;

import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class jisuanqi extends JFrame implements ActionListener {
    private JMenuBar menubar;//菜单
    private JMenu menu;//菜单栏
    private JMenuItem standard, science;//菜单栏选项
    private JTextField displayField;//显示区域
    private JTextField jisuanField;//计算区域
    private JPanel currentPanel; // 当前面板
    private final String[] buttonLabels = {
            "%", "CE", "C", "X",
            "1/x", "x^2", "√x", "÷",
            "7", "8", "9", "×",
            "4", "5", "6", "-",
            "1", "2", "3", "+",
            "+/-", "0", ".", "="
    };
    private final String[] JbuttonLabels = {
            "2^nd", "π", "e", "CE", "X",
            "x^2", "1/x", "|x|", "exp", "mod",
            "√", "(", ")", "n!", "÷",
            "x^y", "7", "8", "9", "×",
            "10^x", "4", "5", "6", "-",
            "log", "1", "2", "3", "+",
            "ln", "+/-", "0", ".", "="
    };
    public void kexuePanle() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(7, 5, 5, 5)); // 7行5列的网格布局
        // 创建按钮并添加到面板
        for (String label : JbuttonLabels) {
            JButton button = new JButton(label);
            button.addActionListener(new ButtonClickListener());
            if ("=".equals(label)) {
                button.setForeground(Color.WHITE);
                button.setBackground(Color.decode("#005FBA"));
            } else {
                button.setForeground(Color.BLACK);
                button.setBackground(Color.decode("#F6F9FC"));
            }
            buttonPanel.add(button);
        }
        // 将按钮面板添加到窗口的南部
        currentPanel = buttonPanel; // 设置当前面板为科学计算器面板
        add(currentPanel, BorderLayout.CENTER);
    }

    public void biaozhunPanle() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(6, 4, 5, 5)); // 6行4列的网格布局
        // 创建按钮并添加到面板
        for (String label : buttonLabels) {
            JButton button = new JButton(label);
            button.addActionListener(new ButtonClickListener());
            if ("=".equals(label)) {
                button.setForeground(Color.WHITE);
                button.setBackground(Color.decode("#005FBA"));
            } else {
                button.setForeground(Color.BLACK);
                button.setBackground(Color.decode("#F6F9FC"));
            }
            buttonPanel.add(button);
        }
        // 将按钮面板添加到窗口的南部
        currentPanel = buttonPanel; // 设置当前面板为标准计算器面板
        add(currentPanel, BorderLayout.CENTER);
    }

    public jisuanqi() {
        setTitle("计算器");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        //菜单
        menubar = new JMenuBar();
        standard = new JMenuItem("标准");
        science = new JMenuItem("科学");
        menu = new JMenu("≡");
        menu.add(standard);
        menu.add(science);
        menubar.add(menu);
        setJMenuBar(menubar);
        standard.addActionListener(this);
        science.addActionListener(this);
        //创建计算区域的文本框
        jisuanField = new JTextField();
        int jisuanSize = 16;
        Font jisuanFont = new Font("SansSerif", Font.BOLD, jisuanSize);
        jisuanField.setFont(jisuanFont);
        jisuanField.setEditable(false);
        jisuanField.setHorizontalAlignment(JTextField.RIGHT);
        jisuanField.setBackground(Color.WHITE);
        // 创建显示区域的文本框
        displayField = new JTextField();
        int fontsize = 48;
        Font font = new Font("SansSerif", Font.BOLD, fontsize);
        displayField.setFont(font);
        displayField.setEditable(false);
        displayField.setHorizontalAlignment(JTextField.RIGHT);
        displayField.setBackground(Color.WHITE);
        displayField.setText("0");
        //计算面板
        JPanel jisuanPanel = new JPanel();
        jisuanPanel.setLayout(new BoxLayout(jisuanPanel, BoxLayout.Y_AXIS));
        jisuanPanel.add(jisuanField);
        jisuanPanel.add(displayField);
        add(jisuanPanel, BorderLayout.NORTH);
        //默认标准面板
        biaozhunPanle();
        setSize(400, 400);
        setVisible(true);
    }

    //切换面板
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == standard) {
            if (currentPanel != null) {
                remove(currentPanel); // 移除当前面板
            }
            biaozhunPanle(); // 添加标准面板
            validate(); // 验证框架的布局
            repaint(); // 重绘框架
        } else if (e.getSource() == science) {
            if (currentPanel != null) {
                remove(currentPanel); // 移除当前面板
            }
            kexuePanle(); // 添加科学面板
            validate(); // 验证框架的布局
            repaint(); // 重绘框架
        }
    }

    // 按钮点击事件监听器
    private class ButtonClickListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if ("%".equals(command)) {

            } else if ("X".equals(command)) {
                // 删除最后一个字符的逻辑
                String text = displayField.getText();
                if (text.length() > 1) {
                    displayField.setText(text.substring(0, text.length() - 1));
                } else {
                    displayField.setText("0");
                }
            } else if ("1/x".equals(command)) {
                double x;
                if (displayField.getText().contains(".")) {
                    x = Double.parseDouble(displayField.getText());
                } else {
                    x = Integer.parseInt(displayField.getText());
                }
                if (x == 0) {
                    displayField.setText("分母不能为0");
                } else {
                    displayField.setText(String.valueOf(1 / x));
                }
            } else if ("x^2".equals(command)) {
                double x;
                if (displayField.getText().contains(".")) {
                    x = Double.parseDouble(displayField.getText());
                } else {
                    x = Integer.parseInt(displayField.getText());
                }
                displayField.setText(String.valueOf(Math.pow(x, 2)));
            } else if ("√x".equals(command)) {
                double x;
                if (displayField.getText().contains(".")) {
                    x = Double.parseDouble(displayField.getText());
                } else {
                    x = Integer.parseInt(displayField.getText());
                }
                displayField.setText(String.valueOf(Math.sqrt(x)));
            } else if ("+/-".equals(command)) {
                if (displayField.getText().charAt(0) == '-') {
                    displayField.setText(displayField.getText().substring(1));
                } else {
                    displayField.setText('-' + displayField.getText());
                }
            } else if (".".equals(command)) {
                if (displayField.getText().contains(".")) {
                } else {
                    displayField.setText(displayField.getText() + command);
                }
            } else if ("+".equals(command) || "-".equals(command)) {
                jisuanField.setText(displayField.getText() + command);
            } else if ("×".equals(command)) {
                jisuanField.setText(displayField.getText() + '*');
            } else if ("÷".equals(command)) {
                jisuanField.setText(displayField.getText() + '/');
            } else if ("C".equals(command)) {
                //清楚显示区域和计算区域
                displayField.setText("0");
                jisuanField.setText("");
            } else if ("CE".equals(command)) {
                // 清楚显示区域
                displayField.setText("0");
            } else if ("=".equals(command)) {
                if (jisuanField.getText().contains("=")) {

                    displayField.setText(String.valueOf(jisuan(jisuanField.getText())));
                } else {
                    // 计算操作
                    jisuanField.setText(jisuanField.getText() + displayField.getText() + command);
                    displayField.setText(String.valueOf(jisuan(jisuanField.getText())));
                }
            } else {
                // 添加按钮文本到显示区域
                if (jisuanField.getText().contains("+") || jisuanField.getText().contains("-") || jisuanField.getText().contains("×") || jisuanField.getText().contains("÷")) {
                    if (displayField.getText().equals("0")) {
                        displayField.setText(command);
                    } else if (displayField.getText().contains(".")) {
                        displayField.setText(displayField.getText() + command);
                    } else {
                        displayField.setText(command);
                    }
                } else if ("0".equals(displayField.getText())) {
                    displayField.setText(command);
                } else if (displayField.getText().contains(".")) {
                    displayField.setText(displayField.getText() + command);
                } else if (jisuanField.getText().isEmpty()) {
                    displayField.setText(displayField.getText() + command);
                }
            }
        }
    }

    //计算
    private double jisuan(String str) {
        Expression expression = new ExpressionBuilder(str.substring(0, str.length() - 1)).build();
        double result = expression.evaluate();
        System.out.println(result); // 输出
        return result;
    }

    public static void main(String[] args) {
        new jisuanqi();
    }
}
